package com.starleague.jpa.apengine;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.starleague.jpa.appengine.GaePersistManager;

public class GaeTestHelper
{
    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
    private final String persistenceUnitName;
    private GaePersistManager persistManager;

    public GaeTestHelper(String persistenceUnitName)
    {
        this.persistenceUnitName = persistenceUnitName;
    }

    public synchronized GaePersistManager getPersistManager()
    {
        return this.persistManager;
    }

    public synchronized void resetPersistManager()
    {
        this.persistManager.close();
        this.persistManager = new GaePersistManager(this.persistenceUnitName);
    }

    public void setUp() throws Exception
    {
        // super.setUp();
        this.helper.setUp();

        this.persistManager = new GaePersistManager(this.persistenceUnitName);

        // this.entityManagerFactory = Persistence
        // .createEntityManagerFactory(DEFAULT_PERSISTENCE_UNIT);
        // this.entityManager = this.entityManagerFactory.createEntityManager();
        // this.entityManager.setFlushMode(FlushModeType.COMMIT);
        // this.transaction = this.entityManager.getTransaction();
        // this.transaction.begin();

        // log.debug("SETUP");
    }

    public void tearDown()
    {
        // log.debug("TEARDOWN");

        // if (this.transaction.isActive())
        // this.transaction.rollback();
        //
        // this.entityManager.close();
        // this.entityManagerFactory.close();
        this.persistManager.close();

        this.helper.tearDown();

        // super.tearDown();
    }

    // public void close()
    // {
    // if ((this.transaction != null) && this.transaction.isActive())
    // this.transaction.rollback();
    //
    // if (this.entityManager.isOpen())
    // this.entityManager.close();
    // }
    //
    // public synchronized boolean startTransaction()
    // {
    // if ((this.transaction != null) && this.transaction.isActive())
    // return false;
    //
    // this.transaction = this.entityManager.getTransaction();
    // this.transaction.begin();
    // return true;

    // this.entityManager = entityManagerFactory.createEntityManager();
    // this.entityManager.setFlushMode(FlushModeType.COMMIT);
    // }
    //
    // //
    // // Transaction management
    // //
    //
    // public synchronized boolean startTransaction()
    // {
    // if ((this.transaction != null) && this.transaction.isActive())
    // return false;
    //
    // this.transaction = this.entityManager.getTransaction();
    // this.transaction.begin();
    // return true;

    //
    //
    //

    // protected GameInstanceDao.Proxy createGameInstance(final String i_name)
    // throws StarLeagueException
    // {
    // GameInstanceDao.Proxy gameInstance =
    // GameInstanceDao.Proxy.lookupByName(this.persistManager, i_name);
    // assertNull(gameInstance);
    //
    // gameInstance = GameInstanceDao.Proxy.create(this.persistManager, i_name);
    // assertEquals(i_name, gameInstance.getName());
    // assertNotNull(gameInstance.getDao().getIdKey());
    //
    // return gameInstance;
    // }
    //
    // protected GameInstanceDao.Proxy lookupGameInstance(final String i_name)
    // throws Throwable
    // {
    // final GameInstanceDao.Proxy gameInstance =
    // GameInstanceDao.Proxy.lookupByName(this.persistManager,
    // i_name);
    //
    // if (gameInstance != null)
    // {
    // assertEquals(i_name, gameInstance.getName());
    // assertNotNull(gameInstance.getDao().getIdKey());
    // }
    //
    // return gameInstance;
    // }
    //
    // protected GameInstanceDao findGameInstanceDao(final Key
    // i_gameInstanceKey) throws Throwable
    // {
    // final GameInstanceDao gameInstanceDao =
    // this.persistManager.find(GameInstanceDao.class,
    // i_gameInstanceKey);
    //
    // if (gameInstanceDao != null)
    // {
    // assertNotNull(gameInstanceDao.getIdKey());
    // assertEquals(i_gameInstanceKey, gameInstanceDao.getIdKey());
    // assertEquals(0, gameInstanceDao.getIdKey().compareTo(i_gameInstanceKey));
    // }
    //
    // return gameInstanceDao;
    // }
    //
    // protected GameInstanceDao findGameInstanceDao(final GameInstanceDao.Proxy
    // i_gameInstance) throws
    // Throwable
    // {
    // final GameInstanceDao gameInstanceDao =
    // this.findGameInstanceDao(i_gameInstance.getIdKey());
    //
    // if (gameInstanceDao != null)
    // {
    // assertEquals(i_gameInstance.getName(), gameInstanceDao.getName());
    // }
    //
    // return gameInstanceDao;
    // }
}
